package com.codi.bus.core.service.organ.impl;

import com.codi.base.exception.ExceptionHandler;
import com.codi.base.util.DateUtil;
import com.codi.base.util.DateUtils;
import com.codi.base.util.EqualsUtil;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.domain.OrganProfile;
import com.codi.bus.core.service.organ.OrganProfileService;
import com.codi.fcloud.ufx.constant.HsConst;
import com.codi.message.constant.ErrorConst;
import com.codi.superman.base.service.SysDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 机构资料
 *
 * @author shi.pengyan
 * @version 1.0 2017-09-01 9:16
 * @since 1.0
 */
@Slf4j
@Service("organProfileService")
@Transactional
public class OrganProfileServiceImpl implements OrganProfileService {

    @Autowired
    private OrganProfileDao organProfileDao;

    @Autowired
    private SysDBService sysDBService;


    @Override
    public OrganProfile getById(Long profileId){
        return organProfileDao.selectByPrimaryKey(profileId);
    }


    @Override
    public OrganProfile addOrUpdateByProfileId(OrganProfile record) {

        if (record.getProfileId() == null) {

            log.info("organ profile add");
            organProfileDao.insertSelective(record);
        } else {

            log.info("organ profile update");
            organProfileDao.updateByPrimaryKeySelective(record);
        }

        return record;
    }

    @Override
    public OrganProfile qryByUserId(Long userId) {
        return organProfileDao.qryByUserId(userId);
    }

    @Override
    public OrganProfile qryByProfileId(Long profileId) {
        return organProfileDao.qryByProfileId(profileId);
    }

    @Override
    public OrganProfile queryByAgentId(Long agentId) {
        return organProfileDao.queryByAgentId(agentId);
    }

    @Override
    public List<OrganProfile> queryListByUserId(Long userId) {

        return organProfileDao.queryListByUserId(userId);
    }

    @Override
    public List<OrganProfile> queryListByAgentId(Long agentId) {
        return organProfileDao.queryListByAgentId(agentId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int updateState(Long profileId, int state) {

        OrganProfile record = new OrganProfile();
        record.setProfileId(profileId);
        record.setState(state);
        record.setLastUpdatedDate(new Date());

        return organProfileDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public String updateUserRiskLevelExpireTime(Long userId, String effectTimeStr) {
        if (StringUtil.isEmpty(effectTimeStr)) {
            log.warn("user risk level effect time is null, plz check!");
            return "";
        }

        Date expireTime = DateUtils.addMondth(DateUtil.parse(effectTimeStr, GlobalConstant.DATE_FORMAT_YYYYMMDD),
            HsConst.USER_RISK_LEVEL_EFFECT_YEAR * 12);
        String expireTimeStr = DateUtil.format(expireTime, GlobalConstant.DATE_FORMAT_YYYYMMDD);
        log.info("effectTime={},expireTime={}", effectTimeStr, expireTimeStr);


        OrganProfile record = new OrganProfile();
        record.setUserId(userId);
        record.setTestExpiredDate(expireTime);

        organProfileDao.updateTestExpireDate(record);

        return expireTimeStr;
    }

    @Override
    public boolean checkBizLicenseExist(String businessLicence) {
        return organProfileDao.checkBusinessLicenseExist(businessLicence);
    }

    @Override
    public boolean checkBizLicenseExist(String businessLicence, Long profileId) {
        return organProfileDao.checkBusinessLicenseExist(businessLicence, profileId);
    }

    @Override
    public Page<OrganProfile> query(OrganProfile record, Pageable pageable) {
        return organProfileDao.query(record, pageable);
    }

    @Override
    public void confirmPassNotifyUser(Long userId, Long profileId) {

        OrganProfile dto = new OrganProfile();
        if (profileId != null){
            dto = qryByProfileId(profileId);
        }else {
            dto = qryByUserId(userId);
        }
        if (dto == null) {
            log.warn("canot find this org profile[userId={}]", userId);
            return;
        }

        if (EqualsUtil.equals(dto.getState().intValue(), GlobalConstant.ORGAN_STATE_PASS)) {

        } else {
            ExceptionHandler.publish(ErrorConst.ORGAN_AUDIT_NOT_PASS_TIP);
        }


        OrganProfile record = new OrganProfile();
        record.setProfileId(dto.getProfileId());
        record.setPassHasNotifyUser(1);
        record.setPassNotifyDate(new Date());
        organProfileDao.updateByPrimaryKeySelective(record);
    }

}
